﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace T4ViewModel
{
    public class ViewModelSetupCollection
    {
        private List<IViewModelSetup> configurations = new List<IViewModelSetup>();
        private string NamespaceName { get; set; }
       

        /// <summary>
        /// Generates view model for specified model type
        /// </summary>
        /// <typeparam name="ModelType">Type of model which the view model should be generated for</typeparam>
        /// <param name="viewModelName">Name of generated view model</param>
        /// <returns></returns>
        public ViewModelSetup<ModelType> For<ModelType>(string viewModelName)
            where ModelType : new()
        {
            var setup = new ViewModelSetup<ModelType>();
            configurations.Add(setup);
            setup.Name = viewModelName;
            return setup;
        }

        /// <summary>
        /// Sets namespace for generated view models
        /// </summary>
        /// <param name="namespaceName">Name of the namespace</param>
        public void SetNamespace(string namespaceName)
        {
            NamespaceName = namespaceName;
        }


        internal string GenerateCode()
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine("using System;");
            result.AppendLine("using " + typeof(ViewModel<>).Namespace + ";");
            result.AppendLine();

            result.AppendLine("namespace " + (NamespaceName ?? "ViewModels"));
            result.AppendLine("{");

            foreach (var configuration in configurations)
                result.AppendLine(configuration.GenerateCode());

            result.AppendLine("}");
            return result.ToString();
        }
    }
}
